﻿//***********************************
// 文件名(File Name):    Calc
// 数据表(Tables):       nothing
// 作者(author):         汲磊举
// 日期(Create Date):    2016.1.21
//***********************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TestStack.White;
using TestStack.White.UIItems.WindowItems;
using TestStack.White.Factory;
using TestStack.White.UIItems.Finders;
using Application = TestStack.White.Application;
using Button = TestStack.White.UIItems.Button;

namespace Calc
{
    public partial class Form1 : Form
    {
        private const string ExeSourceFile = @"C:\Windows\system32\calc.exe";
        //Global Variable to for Application launch
        private static Application _application;
        //Global variable to get the Main window of calculator from application.
        private static Window _mainWindow;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
             _application = Application.Launch(ExeSourceFile);
             _mainWindow = _application.GetWindow(SearchCriteria.ByText("计算器"), InitializeOption.NoCache);
            
            // 通过Button name实现自动化
            AutoByName(_mainWindow);

            //通过Button AutomationId实现自动化
            AutoByAutomationId(_mainWindow);

        }

        /// <summary>
        /// 通过Name得到窗体按键，自动化操作。
        /// </summary>
        /// <param name="mainwindow">需要自动化操作的主窗体</param>
        public void AutoByName(Window mainwindow)
        {
            Thread.Sleep(1000); //停顿一秒，视觉效果
            // 通过Button Name得到Button，触发点击事件
            Button btn1 = _mainWindow.Get<Button>(SearchCriteria.ByText("1"));
            btn1.Click();
            Thread.Sleep(1000);
            Button btn2 = _mainWindow.Get<Button>(SearchCriteria.ByText("加"));
            btn2.Click();
            Thread.Sleep(1000);
            Button btn3 = _mainWindow.Get<Button>(SearchCriteria.ByText("2"));
            btn3.Click();
            Thread.Sleep(1000);
            Button btn4 = _mainWindow.Get<Button>(SearchCriteria.ByText("等于"));
            btn4.Click();
        }

        /// <summary>
        /// 通过AutomationId得到窗体按键，自动化操作。
        /// </summary>
        /// <param name="mainwindow">需要自动化操作的主窗体</param>
        public void AutoByAutomationId(Window mainwindow)
        {
            Thread.Sleep(1000); //停顿一秒，视觉效果
            // 通过Button AutomationId得到Button，触发点击事件
            Button btn1 = _mainWindow.Get<Button>(SearchCriteria.ByAutomationId("131"));
            btn1.Click();
            Thread.Sleep(1000);
            Button btn2 = _mainWindow.Get<Button>(SearchCriteria.ByAutomationId("93"));
            btn2.Click();
            Thread.Sleep(1000);
            Button btn3 = _mainWindow.Get<Button>(SearchCriteria.ByAutomationId("132"));
            btn3.Click();
            Thread.Sleep(1000);
            Button btn4 = _mainWindow.Get<Button>(SearchCriteria.ByAutomationId("121"));
            btn4.Click();
        }
    }
}
